package com.runen.wnhz.runen.ui.adapter.puadapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.runen.wnhz.runen.R;

import com.runen.wnhz.runen.bean.LessonRelativeInfoBean;

import java.util.List;

public class LessonCatalogAdapter extends BaseAdapter {

    private List<LessonRelativeInfoBean.DataBean.TitleListBean> titleListBeans;

    private Context context;

    public LessonCatalogAdapter(List<LessonRelativeInfoBean.DataBean.TitleListBean> titleListBeans, Context context) {
        this.titleListBeans = titleListBeans;
        this.context = context;
    }

    @Override
    public int getCount() {
        if(titleListBeans != null){
            return titleListBeans.size();
        }else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder viewHolder;
        if(convertView == null){
            convertView = View.inflate(context,R.layout.item_catalog,null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)convertView.getTag();
        }
        LessonRelativeInfoBean.DataBean.TitleListBean titleListBean = titleListBeans.get(position);
        viewHolder.tv_name.setText(titleListBean.getTitle());
        viewHolder.ry_part.setAdapter(new LessonCatalogPartAdapter(titleListBean.getNext_list(),context,titleListBean.getTry_look()));
        viewHolder.iv_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(viewHolder.ry_part.getVisibility() == View.VISIBLE){
                    viewHolder.iv_open.setImageResource(R.mipmap.icon_down);
                    viewHolder.ry_part.setVisibility(View.GONE);
                }else{
                    viewHolder.iv_open.setImageResource(R.mipmap.icon_up);
                    viewHolder.ry_part.setVisibility(View.VISIBLE);
                }
            }
        });
        return convertView;
    }

    private static class ViewHolder {
         TextView tv_name;
         ImageView iv_open;
         ListView ry_part;

        public ViewHolder(View convertView) {
            this.tv_name = convertView.findViewById(R.id.tv_name);
            this.iv_open = convertView.findViewById(R.id.iv_open);
            this.ry_part = convertView.findViewById(R.id.ry_part);
        }
    }
}
